package com.tencent.system.service.impl;

import com.tencent.basic.util.PageList;
import com.tencent.system.domain.SystemDictionaryDetail;
import com.tencent.system.domain.SystemDictionaryType;
import com.tencent.system.mapper.SystemDictionaryDetailMapper;
import com.tencent.system.mapper.SystemDictionaryTypeMapper;
import com.tencent.system.query.SystemDictionaryTypeQuery;
import com.tencent.system.service.ISystemDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class SystemDictionaryTypeServiceImpl implements ISystemDictionaryTypeService {

    @Autowired
    private SystemDictionaryTypeMapper systemDictionaryTypeMapper;

    @Autowired
    private SystemDictionaryDetailMapper systemDictionaryDetailMapper;


    /**
     * 业务层实现类 ： 查询所有
     * @return
     */
    @Override
    public List<SystemDictionaryType> queryAll() {
        return systemDictionaryTypeMapper.findAll();
    }

    /**
     * 业务层实现类 ： 查询单个
     * @param id
     * @return
     */
    @Override
    public SystemDictionaryType queryById(Long id) {
        return systemDictionaryTypeMapper.findById(id);
    }

    /**
     * 业务层实现类 ： 删除
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id) {

        List<SystemDictionaryDetail> byId = systemDictionaryDetailMapper.findByTypeId(id);

        if (byId != null) {
            for (SystemDictionaryDetail item : byId) {
                systemDictionaryDetailMapper.delete(item.getId());
            }
        }

        systemDictionaryTypeMapper.delete(id);
    }

    /**
     * 业务层实现类 ： 添加
     * @param systemDictionaryType
     */
    @Transactional
    @Override
    public void save(SystemDictionaryType systemDictionaryType) {
        systemDictionaryTypeMapper.save(systemDictionaryType);
    }

    /**
     * 业务层实现类 ： 修改
     * @param systemDictionaryType
     */
    @Transactional
    @Override
    public void update(SystemDictionaryType systemDictionaryType) {
        systemDictionaryTypeMapper.update(systemDictionaryType);
    }

    /**
     * 业务层实现类 ：分页查询
     * @param query
     * @return
     */
    @Override
    public PageList<SystemDictionaryType> queryPage(SystemDictionaryTypeQuery query) {
        // 先查询总条数 - 传query进去 是高级查询keyword 的需要
        Long totals = systemDictionaryTypeMapper.queryCount(query);
        // 如果没有数据 就返回一个new PageList<> 有默认值 0 和 空的ArrayList<>
        if(totals == 0) {
            return new PageList<>();
        }
        // 如果有数据 执行分页方法 query中有begin pageSize currentPage 获取数据
        List<SystemDictionaryType> data = systemDictionaryTypeMapper.queryData(query);

        // 将 总数和 数据返回 完成分页
        return new PageList<>(totals, data);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Transactional
    @Override
    public void patchDelete(Long[] ids) {

        for (Long aId : ids) {
            List<SystemDictionaryDetail> byId = systemDictionaryDetailMapper.findByTypeId(aId);
            if (byId != null) {
                for (SystemDictionaryDetail item : byId) {
                    systemDictionaryDetailMapper.delete(item.getId());
                }
            }
        }

        systemDictionaryTypeMapper.patchDelete(ids);
    }
}
